<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <tree-tools :is-root="true" :obj="companyInfo" @add-dept="onAdd" />
        <!-- 树形控件 -->
        <el-tree :data="treeData" :props="defaultProps" :default-expand-all="true">
          <tree-tools
            slot-scope="{data}"
            :obj="data"
            @add-dept="onAdd"
            @onEdit="onEdit"
          />
        </el-tree>
      </el-card>
    </div>
    <add-dept ref="dept" v-model="addDialogVisible" :node="treeNode" @on-success="loadCompanyInfo" @edit-success="loadCompanyInfo" />
  </div>
</template>

<script>
import TreeTools from './components/TreeTools.vue'
import { transListToTree } from '@/utils/index'
import { getCompanyInfo } from '@/api/departments.js'
import AddDept from './components/add-dept.vue'
export default {
  components: { TreeTools, AddDept },
  data() {
    return {
      treeData: [],
      defaultProps: {
        label: 'name'
      },
      companyInfo: { name: '传智播客', manager: '负责人', id: '' },
      addDialogVisible: false,
      treeNode: {}
    }
  },

  created() {
    this.loadCompanyInfo()
  },

  methods: {
    async loadCompanyInfo() {
      const res = await getCompanyInfo()
      console.log(res)
      this.treeData = transListToTree(res.depts, '')
    },
    onAdd(data) {
      this.addDialogVisible = true
      this.treeNode = data
    },
    onEdit(data) {
      this.addDialogVisible = true
      // 只为了做校验
      this.treeNode = data
      // 渲染页面
      this.$refs.dept.getDeptsInfo(data.id)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
